package com.quectel.core.module.scheduling.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.scheduling.dao.SchedulingLeaveBalanceLogDao;
import com.quectel.core.module.scheduling.dto.SchedulingLeaveBalanceLogDto;
import com.quectel.core.module.scheduling.entity.SchedulingLeaveBalanceLogEntity;
import com.quectel.core.module.scheduling.service.SchedulingLeaveBalanceLogService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/08/03 11:22
 */
@DubboService
public class SchedulingLeaveBalanceLogServiceImpl implements SchedulingLeaveBalanceLogService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SchedulingLeaveBalanceLogServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> SCHEDULING_LEAVE_BALANCE_LOG_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "SCHEDULING_LEAVE_BALANCE_LOG_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<SchedulingLeaveBalanceLogDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(SCHEDULING_LEAVE_BALANCE_LOG_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private SchedulingLeaveBalanceLogDao schedulingLeaveBalanceLogDao;



    @Override
    public List<SchedulingLeaveBalanceLogDto> selectByPersonAndSourceAndBalance(Long personId, Byte sourceType, Long sourceId, Long balanceId) {
        List<SchedulingLeaveBalanceLogEntity> list = schedulingLeaveBalanceLogDao.selectList(
                new LambdaQueryWrapper<SchedulingLeaveBalanceLogEntity>()
                        .eq(SchedulingLeaveBalanceLogEntity::getPersonId, personId)
                        .eq(SchedulingLeaveBalanceLogEntity::getSourceType, sourceType)
                        .eq(SchedulingLeaveBalanceLogEntity::getSourceId, sourceId)
                        .eq(SchedulingLeaveBalanceLogEntity::getBalanceId, balanceId)
        );
        List<SchedulingLeaveBalanceLogDto> result = CopyUtils.copyList(list, SchedulingLeaveBalanceLogDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public List<SchedulingLeaveBalanceLogDto> selectByPersonAndSource(Long personId, Byte sourceType, Long sourceId) {
        List<SchedulingLeaveBalanceLogEntity> list = schedulingLeaveBalanceLogDao.selectList(
                new LambdaQueryWrapper<SchedulingLeaveBalanceLogEntity>()
                        .eq(SchedulingLeaveBalanceLogEntity::getPersonId, personId)
                        .eq(SchedulingLeaveBalanceLogEntity::getSourceType, sourceType)
                        .eq(SchedulingLeaveBalanceLogEntity::getSourceId, sourceId)
        );
        List<SchedulingLeaveBalanceLogDto> result = CopyUtils.copyList(list, SchedulingLeaveBalanceLogDto.class);
        result.forEach(this::paddingField);
        return result;
    }


    @Override
    public SchedulingLeaveBalanceLogDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                SCHEDULING_LEAVE_BALANCE_LOG_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public SchedulingLeaveBalanceLogDto selectById(Long id) {
        SchedulingLeaveBalanceLogEntity entity = schedulingLeaveBalanceLogDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, SchedulingLeaveBalanceLogDto.class));
    }

    @Override
    public List<SchedulingLeaveBalanceLogDto> queryList(Map<String, Object> params) {
        List<SchedulingLeaveBalanceLogEntity> list = schedulingLeaveBalanceLogDao.queryList(params);
        List<SchedulingLeaveBalanceLogDto> result = CopyUtils.copyList(list, SchedulingLeaveBalanceLogDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public List<SchedulingLeaveBalanceLogDto> queryListWithoutPadding(Map<String, Object> params) {
        List<SchedulingLeaveBalanceLogEntity> list = schedulingLeaveBalanceLogDao.queryList(params);
        return CopyUtils.copyList(list, SchedulingLeaveBalanceLogDto.class);
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private SchedulingLeaveBalanceLogDto paddingField(SchedulingLeaveBalanceLogDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return schedulingLeaveBalanceLogDao.queryTotal(params);
    }

    @Override
    public Long save(SchedulingLeaveBalanceLogDto dto) {
        SchedulingLeaveBalanceLogEntity entity = CopyUtils.copyObj(dto, SchedulingLeaveBalanceLogEntity.class);
        entity.setId(Snowflake.nextId());
        schedulingLeaveBalanceLogDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SchedulingLeaveBalanceLogDto dto) {
        SchedulingLeaveBalanceLogEntity entity = CopyUtils.copyObj(dto, SchedulingLeaveBalanceLogEntity.class);

        schedulingLeaveBalanceLogDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(SchedulingLeaveBalanceLogDto dto) {
        SchedulingLeaveBalanceLogEntity entity = CopyUtils.copyObj(dto, SchedulingLeaveBalanceLogEntity.class);

        MyBatisPlusUtils.updateAllColumnById(entity, schedulingLeaveBalanceLogDao);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id) {

        CLEAR_CACHE_FUNC.accept(selectById(id));

        schedulingLeaveBalanceLogDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
